<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Mycommon {	
	
	function getSaveSqlStr($str)
	{
		if(get_magic_quotes_gpc())
		{
			return $str;
		}
		else
		{
			//return mysql_real_escape_string($str);
			$CI = &get_instance();
			return mysqli_real_escape_string($CI->db->conn_id, $str);
		}
	}
    
	function validEmail($email)
    {
		return ( ! preg_match("/^([a-z0-9\+_\-]+)(\.[a-z0-9\+_\-]+)*@([a-z0-9\-]+\.)+[a-z]{2,6}$/ix", $email)) ? false : true;
	}
	
	function validUsername($str) 
	{
	    return preg_match('/^([a-zA-Z])([a-z0-9_])*$/',$str) ? true : false;
	}
    function validMd5($str)
    {
    	$str = trim($str);
    	if($str == '')
    	{
    		return false;
    	}
    	$chk = (preg_match('/^[a-f0-9]{32}$/i', $str)) ? true : false;
    	return $chk;
    }
    /*---------GET CURRENT_URL------------*/
    function selfURL()
    {
        $s = empty($_SERVER["HTTPS"]) ? '' : ($_SERVER["HTTPS"] == "on") ? "s" : "";
        $protocol = $this->strleft(strtolower($_SERVER["SERVER_PROTOCOL"]), "/").$s;
        $port = ($_SERVER["SERVER_PORT"] == "80") ? "" : (":".$_SERVER["SERVER_PORT"]);
        return $protocol."://".$_SERVER['SERVER_NAME'].$port.$_SERVER['REQUEST_URI'];
    }
    function strleft($s1, $s2)
    {
        return substr($s1, 0, strpos($s1, $s2));
    }
    /*----GET STORE FROM MODEL------------*/
    function getNameStore($strCallStore)
    {
        $arrStr = explode("(", $strCallStore);
        return $arrSt = substr(trim($arrStr[0]), 4);
    }
    /*--------------------NICE TIME---------------------*/
	function nicetime($date)
	{
		if(empty($date)) {
			return "No date provided";
		}
	   
		$periods         = array("giây", "phút", "giờ", "ngày", "tuần", "tháng", "năm", "thập kỷ");
		$lengths         = array("60","60","24","7","4.35","12","10");
	   
		$now             = time();
		$unix_date         = strtotime($date);
	   
		   // check validity of date
		if(empty($unix_date)) {   
			return "Ngày đểu";
		}
	
		// is it future date or past date
		if($now > $unix_date)
		{   
			$difference     = $now - $unix_date;
			$tense         = "trước";
		   
		}
		else
		{
			$difference     = $unix_date - $now;
			$tense         = "nữa";
		}
		for($j = 0; $difference >= $lengths[$j] && $j < count($lengths)-1; $j++) {
			$difference /= $lengths[$j];
		}
		$difference = round($difference);
	   
		if($difference != 1) {
			//$periods[$j].= "s";
		}
	   
		return "$difference $periods[$j] {$tense}";
	}
}